package com.dnjn.happiness.emo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.biz.api.RemoteCommunicateService;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.api.model.EmoUserBean;
import com.dnjn.happiness.emo.config.HappinessEmoConfig;
import com.dnjn.happiness.emo.domain.User;
import com.dnjn.happiness.emo.mapper.UserMapper;
import com.dnjn.happiness.emo.service.IUserService;
import com.dnjn.happiness.system.api.domain.UserSituationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 家庭记录Service业务层处理
 *
 * @author happiness
 * @date 2023-10-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private HappinessEmoConfig config;
    @Autowired
    private RemoteCommunicateService communicateService;

    @Override
    public Boolean updateHappinessScores(Long userId, Integer addScore) {

//        Integer oldScore = baseMapper.selectEmoUserHappinessScores(userId);
        User one = lambdaQuery().eq(User::getUserId, userId).eq(User::getDeleted, Constants.UNDELETED).one();
        if (null == one) {
            return false;
        }

        Integer oldScore = lambdaQuery().eq(User::getUserId, userId).eq(User::getDeleted, Constants.UNDELETED).one().getHappinessScore().intValue();

        Integer happinessScores = oldScore + addScore;

        // 判断最大值最小值
        if (happinessScores >= config.getMaxScore()) {
            happinessScores = config.getMaxScore();
        }
        if (happinessScores <= config.getMiniScore()) {
            happinessScores = config.getMiniScore();
        }

        baseMapper.updateHappinessScores(userId, happinessScores);
        communicateService.handleScoreEvent(userId, happinessScores, SecurityConstants.INNER);
        return  true;
    }

    @Override
    public void emptytheHappyBeans(Long userId) {
        baseMapper.emptytheHappyBeans(userId);
    }

    @Override
    public void emptytheHappyBeansToday(Long userId) {
        baseMapper.emptytheHappyBeansToday(userId);
    }

    @Override
    public User saveEmoUser(Long userId) {
        User emoUser = new User();
        emoUser.setUserId(userId);
        emoUser.setHlbean(0L);
        emoUser.setHappinessScore(0L);
        emoUser.setHlbeanToday(0L);
        save(emoUser);
        return emoUser;
    }

    @Override
    public List<User> selectUserList() {
        return baseMapper.selectUserList();
    }

    @Override
    public List<EmoUserBean> selectByLimit(int index, int num) {
        return baseMapper.selectByLimit(index, num);
    }

    @Override
    public Integer selectRequiredCommunicateCount(Integer startInterval, Integer endInterval, int criticalValue,Long deptId) {
        return baseMapper.selectRequiredCommunicateCount(startInterval, endInterval, criticalValue,deptId);
    }

    @Override
    public Integer selectnormalCount(Integer startInterval, Integer endInterval, int criticalValue,Long deptId) {
        return baseMapper.selectnormalCount(startInterval, endInterval, criticalValue,deptId);
    }

    @Override
    public Page<UserSituationVO> getHomePage(Page page, HashMap<String, Object> map) {
        return baseMapper.getHomePage(page, map);
    }

    @Override
    public Integer selectDelayDaysByUserId(Long userId) {
        return baseMapper.selectDelayDaysByUserId(userId);
    }

    @Override
    public boolean updateWorkDate(Long userId) {
        int cc = baseMapper.updateWorkDate(userId);
        return true;

    }

    @Override
    public void updateEmoUserBeansToZero() {
        baseMapper.updateToZero();
    }

}
